package com.example.asus.a222dzd_ordering.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.asus.a222dzd_ordering.R;
import com.example.asus.a222dzd_ordering.adapter.FoodListAdapter;
import com.example.asus.a222dzd_ordering.base.BaseListner;
import com.example.asus.a222dzd_ordering.base.BaseModel;
import com.example.asus.a222dzd_ordering.bean.FoodListBean;
import com.example.asus.a222dzd_ordering.bean.UserBean;


import java.util.List;

import retrofit2.Call;

public class FoodListActivity extends AppCompatActivity{

    private RecyclerView recyclerView;
    private List<FoodListBean> beans;
    private FoodListAdapter adapter;
    private int shop_id,userid;
    private ImageView iv_foodlist_collect;
    private SharedPreferences sharedPreferences;
    private boolean iscollect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
        Intent getIntent = getIntent();
        shop_id=getIntent.getIntExtra("shop_id",0);
        initView();
        initData();
        initEvent();
    }

    private void initView(){
        iv_foodlist_collect=findViewById(R.id.iv_foodList_collect);
        adapter = new FoodListAdapter();
        recyclerView = findViewById(R.id.rv_foodlist);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    BaseListner<List<FoodListBean>> listner = new BaseListner<List<FoodListBean>>() {
        @Override
        public void onResponse(List<FoodListBean> shopBeans) {
            beans=shopBeans;
            if (beans==null)
                Toast.makeText(getApplicationContext(),"Kong", Toast.LENGTH_LONG).show();
            else{
                adapter.setData(beans);

            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
        }
    };

    private void initData(){
        sharedPreferences = getSharedPreferences("User",MODE_PRIVATE);
        userid = Integer.parseInt(sharedPreferences.getString("userid","获取id失败"));
        isCollect();
        BaseModel<List<FoodListBean>> model = new BaseModel<>();
        model.callEnqueue(model.service.getFoodByShop(shop_id),listner);

    }

    private void initEvent(){
        iv_foodlist_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCollect();
            }
        });
        adapter.setOnItemClickListener(new FoodListAdapter.OnItemClickListener() {
            @Override
            public void setItem(View view, int position) {
                FoodListBean bean = beans.get(position);
                Intent intent = new Intent(getApplicationContext(),FoodContentActivity.class);

                intent.putExtra("foodname",bean.getFoodname());
                intent.putExtra("foodprice",bean.getPrice());
                intent.putExtra("foodpic",bean.getPic());
                intent.putExtra("foodid",bean.getFood_id());
                startActivity(intent);
            }
        });
    }

    //判断是否收藏
    private void isCollect(){
        BaseModel<UserBean> model = new BaseModel<>();
        Call<UserBean> call = model.service.isCollected(userid,shop_id,0);
        model.callEnqueue(call, new BaseListner<UserBean>() {
            @Override
            public void onResponse(UserBean userBean) {
                if (userBean.getCollected().equals("0")) {
                    iscollect = false;
                    iv_foodlist_collect.setImageResource(R.drawable.ic_star_border);
                }
                else {
                    iscollect = true;
                    iv_foodlist_collect.setImageResource(R.drawable.ic_star_yellow_24dp);
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(FoodListActivity.this,msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
    //收藏/取消收藏
    private void doCollect(){
        BaseModel<UserBean> model = new BaseModel<>();
        model.callEnqueue(model.service.doCollectShop(userid, shop_id), new BaseListner<UserBean>() {
            @Override
            public void onResponse(UserBean userBean) {
                if(userBean.getSuccess().equals("0"))
                    Toast.makeText(FoodListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                else {
                    if(iscollect) {
                        iscollect = false;
                        iv_foodlist_collect.setImageResource(R.drawable.ic_star_border);
                        Toast.makeText(FoodListActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        iscollect = true;
                        iv_foodlist_collect.setImageResource(R.drawable.ic_star_yellow_24dp);
                        Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(FoodListActivity.this,msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
